<?php

namespace app\controller;
use app\BaseController;
use app\model\SysAccount;
use app\model\SysAccount\SysAccountDepart;
use app\model\SysDepartment;
use app\model\SysDepartment\SysDepartmentRole;
use app\model\SysRole;
use app\utils\CamlHump;
use app\utils\ResposeUtils;
use think\db\Builder;
use think\facade\Db;
use think\facade\Log;
use think\facade\Request;
use Underscore\Types\Arrays;

class SysDepartmentController  extends BaseController
{
    public function list()
    {
        $body = Request::post();
        $query = SysDepartment::createQuery($body);
        $list= CamlHump::toCamlArray($query->select());
        /*haveNext*/
        for ($i=0;$i<count($list);$i++){
            $list[$i]['haveNext'] = SysDepartment::haveChildByParentId($list[$i]['id'] ?? '');


            //departmentRole
            $departRoleList=CamlHump::toCamlArray(SysDepartmentRole::createQuery([
                "departId" => $list[$i]['id']
            ])->select());
            for ($j = 0; $j < count($departRoleList); $j++) {
                $role=$departRoleList[$j];
                $roleOne= SysRole::findOne($role['roleId']);
                $departRoleList[$j]['roleName']= $roleOne['name'];
                $departRoleList[$j]['roleOne']=$roleOne;
            }


            $list[$i]['departmentRoleList']= $departRoleList;



        }
        return ResposeUtils::ok(
            $list ,
            200,
            $query->count()
        );
    }
    function getbyid()
    {
        $id = Request::get("id");
        $one = SysDepartment::findOne($id);
        return ResposeUtils::ok(
            $one
        );
    }

    function update()
    {
        $body = Request::post();
        $updateersuilt=SysDepartment::updatePlus($body,$body['id']);
        return ResposeUtils::ok(
            $updateersuilt['id']
        );
    }
    function add()
    {
        $body = Request::post();
        $result=SysDepartment::addPlus($body);
        return ResposeUtils::ok(
            $result['id']
        );
    }
    function delbyid()
    {
        $id = Request::get('id');
        $result=SysDepartment::destroy($id);
        return ResposeUtils::ok(
            $result
        );
    }

    function getAccountUserlistByDepartId(){
        $departId = Request::get('departId');
        $query = Db::table('sys_account_depart');
        $query->where('depart_id', $departId);
        $query->where('del_state', 1);
        //暂时不分页
       $datalist = CamlHump::toCamlArray($query->select());
        for ($i = 0; $i < count($datalist); $i++) {
            $accountId = $datalist[$i]['accountId'];
            if(!isset($accountId)){
                continue;
            }
            $one = SysAccount::findOne($accountId);
            if(!isset($one)||empty($one)){
                continue;
            }

            $datalist[$i]['accountName'] = $one['userName'];
            $datalist[$i]['userOne'] = $one;


        }




       return ResposeUtils::ok(
           $datalist
       );
    }

    function deleteAccountDepartBinding(){
        $accountDepartId = Request::get('accountDepartId');
        if(!isset($accountDepartId)){
            return ResposeUtils::failRes("accountDepartId 必填 ");
        }
        $result = SysAccountDepart::deletePlus($accountDepartId);
        return ResposeUtils::ok(
            $result
        );
    }

    /*添加用户到部门表

    */
    function bindAccountListIntoDepart(){
        $sys_account_depart_list = Request::post();
        /*检查是否已经存在*/
        $newNeedAddList = [];
        for ($i = 0; $i < count($sys_account_depart_list); $i++) {
            if(isset($sys_account_depart_list[$i]['id'])){
                continue;
            }
                   $exittrue = Db::name('sys_account_depart')
                           ->where("del_state", 1)
                    ->where("account_id", $sys_account_depart_list[$i]['accountId'])
                    ->where("depart_id", $sys_account_depart_list[$i]['departId'])

                    ->count() > 0;
                        if(!$exittrue){
                            array_push($newNeedAddList,SysAccountDepart::build($sys_account_depart_list[$i]));
                        }

                 }

        $addhandler=new SysAccountDepart();
        $result = $addhandler->saveAll($newNeedAddList);
        return ResposeUtils::ok(
            $result
        );
    }

    /*获取部门的角色*/
    function getDepartRoles(){
        $departId = Request::post('departId');
        $query=SysDepartmentRole::createQuery(["departId"=>$departId]);
        $list = CamlHump::toCamlArray($query->select());


        for ($i = 0; $i < count($list); $i++) {
            $roleOne = SysRole::findOne($list[$i]['roleId']);
            $list[$i]['roleName'] = $roleOne['name'];
            $list[$i]['roleOne'] = $roleOne;
        }


        return ResposeUtils::ok($list);
    }

    /*加入角色  到部门中国*/
    function bindRolesIntoDepart(){
        $roleListIntoDepart = Request::post();
        if(empty($roleListIntoDepart)){
            return ResposeUtils::failRes("没有 数据");
        }
        Arrays::each($roleListIntoDepart, function ($roleDepart) {
  //幂等 判断是否已经存在
            $isexit=SysDepartmentRole::exitOne(SysDepartmentRole::createQuery([
                "roleId" => $roleDepart['roleId'],
                "departId" => $roleDepart['departId'],
            ]));
            if(!$isexit){
                SysDepartmentRole::addPlus([
                    "roleId" => $roleDepart['roleId'],
                    "departId" => $roleDepart['departId'],
                ]);
            }
        });

        return ResposeUtils::ok([]);
    }

    function deleteRoleFromDepart(){
        $roleDepartId = Request::post("roleDepartId");
        if(!isset($roleDepartId)){
            return ResposeUtils::failRes("没有 roleDepartId 数据");
        }
        $result=SysDepartmentRole::destroy($roleDepartId);
        return ResposeUtils::ok($result);
    }

}